package vn.ikame.android.truyenf17;

import java.io.IOException;

import vn.ikame.android.handler.CHandler;
import vn.ikame.android.utils.CommonUtils;
import vn.ikame.android.utils.XMLParser;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class Content extends Activity {
	public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
	String content;
	private AdView adView;
	
	
	//download
	private AlertDialog.Builder alert;
	private ProgressDialog mProgressDialog;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.content);
		
		LinearLayout lLinLayout = (LinearLayout) findViewById(R.id.layoutcontent);
	    
	    RelativeLayout.LayoutParams adParams2 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		adParams2.addRule(RelativeLayout.ALIGN_BOTTOM);
		adParams2.addRule(RelativeLayout.CENTER_IN_PARENT);
		
		
		adView = new AdView(this, AdSize.SMART_BANNER, "a1502465d6a93ed");
		adView.loadAd(new AdRequest());

		lLinLayout.addView(adView, adParams2);

			if(CommonUtils.isNetworkConnected(this)){
				((TextView)findViewById(R.id.chap_title)).setText(CHandler.storyName + "\n" + CHandler.chapName);
				
				WebView wv = (WebView) findViewById(R.id.webview);
				wv.setBackgroundColor(Color.TRANSPARENT);
				wv.setBackgroundDrawable(null);
				wv.setBackgroundResource(0);
				wv.loadUrl("http://data.ikame.vn/f17/index.php?sid=" + CHandler.chapID);
				
				try{
					StoryList.detailsTracker.trackPageView("ReadChap_" + CHandler.storyName + " - " + CHandler.chapName);
					StoryList.detailsTracker.dispatch();
				}catch (Exception e) {
					// TODO: handle exception
				}
				
	        } else {
	        	alert = new AlertDialog.Builder(this);
	        	alert.setMessage("Vui lòng kiểm tra lại kết nối mạng");
	    		alert.setPositiveButton("Thoát chương trình",
	    				new DialogInterface.OnClickListener() {
	    					public void onClick(DialogInterface dialog,
	    							int whichButton) {
	    						Content.this.finish();
	    						System.gc();
	    						android.os.Process.killProcess(android.os.Process.myPid());
	    					}
	    				});
	    		alert.setNegativeButton("Đóng",
	    				new DialogInterface.OnClickListener() {
	    					public void onClick(DialogInterface dialog,
	    							int whichButton) {
	    					}
	    				});
	    		
	    		alert.setCancelable(false);
	    		alert.show();
	        }
		
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.layout.menu, menu);
	    return true;
	}
	
	public void createAboutMessage(){
		alert = new AlertDialog.Builder(this);
		alert.setMessage(R.string.about);
		alert.setPositiveButton("Gửi mail",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int whichButton) {
						final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

						/* Fill it with Data */
						emailIntent.setType("plain/text");
						emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"support@ikame.vn"});
						emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Ứng dụng truyện ngắn - f145");
						emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Nội dung: ");

						/* Send it off to the Activity-Chooser */
						startActivity(Intent.createChooser(emailIntent, "Send mail..."));
					}
				});

		alert.setNegativeButton("Đóng",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int whichButton) {
					}
				});
		alert.setCancelable(false);
		alert.show();
    }
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.refresh:
			if(CommonUtils.isNetworkConnected(this)){
				((TextView)findViewById(R.id.chap_title)).setText(CHandler.storyName + "\n" + CHandler.chapName);
				
				WebView wv = (WebView) findViewById(R.id.webview);
				wv.setBackgroundColor(Color.TRANSPARENT);
				wv.setBackgroundDrawable(null);
				wv.setBackgroundResource(0);
				wv.loadUrl("http://data.ikame.vn/f17/index.php?sid=" + CHandler.chapID);
	        } else {
	        	alert = new AlertDialog.Builder(this);
	        	alert.setMessage("Vui lòng kiểm tra lại kết nối mạng");
	    		alert.setPositiveButton("Thoát chương trình",
	    				new DialogInterface.OnClickListener() {
	    					public void onClick(DialogInterface dialog,
	    							int whichButton) {
	    						Content.this.finish();
	    						System.gc();
	    						android.os.Process.killProcess(android.os.Process.myPid());
	    					}
	    				});
	    		alert.setNegativeButton("Đóng",
	    				new DialogInterface.OnClickListener() {
	    					public void onClick(DialogInterface dialog,
	    							int whichButton) {
	    					}
	    				});
	    		
	    		alert.setCancelable(false);
	    		alert.show();
	        }
			break;
		case R.id.about:
			createAboutMessage();
			break;

		}
		return true;
	}
	
	
	public void initChapContent(){
		try {
			//Tạo task kết nối với server
			new AsyncTask<Void, Void, String>() {
				@Override
				protected void onPreExecute() {
					//Hiển thị thông báo
					showDialog(DIALOG_DOWNLOAD_PROGRESS);
				}

				@Override
				protected String doInBackground(Void... arg0) {
					try {
						content = XMLParser.getcontent("http://data.ikame.vn/f17/index.php?sid=" + CHandler.chapID);
						System.out.println("chapID = " + CHandler.chapID);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return "";
				}
				//Hoàn thành truy vấn
				@Override
				protected void onPostExecute(String result) {
					
					try {
//						((TextView)findViewById(R.id.chap_title)).setText(CHandler.storyName + " - " + CHandler.chapName);
//						((TextView)findViewById(R.id.chap_content)).setText(content);
					} catch (Exception e) {
					}
					
					dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
				}
			}.execute();
		} catch (Exception e) {
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			this.finish();
//			startActivity(new Intent(Content.this, ChapList.class));
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_DOWNLOAD_PROGRESS:
			mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setCancelable(false);
			mProgressDialog.show();
			return mProgressDialog;
		default:
			return null;
		}
	}
	
}
